package widgets;

import android.app.Dialog;
import android.content.Context;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.StringRes;
import androidx.appcompat.widget.AppCompatButton;

import com.zx.disneyholidays.R;
import com.zx.disneyholidays.utils.BaseUtil;

import io.reactivex.annotations.NonNull;

/**
 * Created by Travis on 2017/9/20 下午3:27.
 *
 * @author Travis.Su
 */

public class KidDialogV2 extends Dialog {
    public KidDialogV2(@NonNull Context context) {
        this(context, R.style.dialog);
    }

    public KidDialogV2(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    public void show() {
        super.show();
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.width = BaseUtil.getScreenWidth(getContext()) - BaseUtil.dp2px(getContext(), 50);
        getWindow().setAttributes(layoutParams);
    }

    public void showPermissionView() {
        super.show();
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.width = BaseUtil.getScreenWidth(getContext()) - BaseUtil.dp2px(getContext(), 104);
        getWindow().setAttributes(layoutParams);
    }

    public void showView() {
        Window window = this.getWindow();
        window.setBackgroundDrawableResource(android.R.color.transparent);
        WindowManager.LayoutParams wl = window.getAttributes();
        wl.x = getWindow().getWindowManager().getDefaultDisplay().getWidth();
        wl.y = getWindow().getWindowManager().getDefaultDisplay().getHeight();
        // 以下这两句是为了保证按钮可以水平满屏
//        wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
//        wl.height = ViewGroup.LayoutParams.MATCH_PARENT;

        this.onWindowAttributesChanged(wl);
        super.show();
    }

    public void showAll() {

        Window window = this.getWindow();
        WindowManager.LayoutParams wl = window.getAttributes();
        wl.x = 0;
        wl.y = getWindow().getWindowManager().getDefaultDisplay().getHeight();
        // 以下这两句是为了保证按钮可以水平满屏
        wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
        wl.height = ViewGroup.LayoutParams.MATCH_PARENT;

        this.onWindowAttributesChanged(wl);
        super.show();

    }

    public void showOrigin() {
        super.show();
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    }

    public void showFromBottom() {
        super.show();
        Window window = getWindow();
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes(layoutParams);
    }

    public void showToBottom() {
        super.show();
        Window window = getWindow();
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        //    layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes(layoutParams);
    }

    public static class Builder {
        private Context mContext;
        private int themeId;
        private String title;
        private String content;
        private Spanned contentWeb;
        private int iconId;
        private int iconColorId;
        private int imageId;
        private boolean cancelable = true;
        private String positiveText;
        private View.OnClickListener positiveClickedListener;
        private String negativeText;
        private View.OnClickListener negativeClickedListener;
        private View contentView;
        private View.OnClickListener closeClickedListener;
        private boolean showCloseView;

        public Builder(Context context) {
            mContext = context;
            themeId = R.style.dialog;
        }

        public Builder(Context context, int themeId) {
            mContext = context;
            this.themeId = themeId;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setTitle(@StringRes int id) {
            this.title = mContext.getString(id);
            return this;
        }

        public Builder setContent(String content) {
            this.content = content;
            return this;
        }

        public Builder setContentWeb(Spanned content) {
            this.contentWeb = content;
            return this;
        }

        public Builder setContent(@StringRes int id) {
            this.content = mContext.getString(id);
            return this;
        }

        public Builder setContentView(View view) {
            this.contentView = view;
            return this;
        }

        public Builder setIcon(@StringRes int iconId, @ColorRes int colorId) {
            this.iconId = iconId;
            this.iconColorId = colorId;
            return this;
        }

        public Builder setImage(@DrawableRes int imageId) {
            this.imageId = imageId;
            return this;
        }

        public Builder cancelable(boolean cancelable) {
            this.cancelable = cancelable;
            return this;
        }

        public Builder setPositiveButton(@StringRes int id, View.OnClickListener listener) {
            this.positiveText = mContext.getString(id);
            this.positiveClickedListener = listener;
            return this;
        }

        public Builder setPositiveButton(String text, View.OnClickListener listener) {
            this.positiveText = text;
            this.positiveClickedListener = listener;
            return this;
        }

        public Builder setNegativeButton(@StringRes int id, View.OnClickListener listener) {
            this.negativeText = mContext.getString(id);
            this.negativeClickedListener = listener;
            return this;
        }

        public Builder setNegativeButton(String text, View.OnClickListener listener) {
            this.negativeText = text;
            this.negativeClickedListener = listener;
            return this;
        }

        public Builder showCloseButton() {
            return showCloseButton(null);
        }

        public Builder showCloseButton(View.OnClickListener listener) {
            this.closeClickedListener = listener;
            this.showCloseView = true;
            return this;
        }

        public KidDialogV2 showOrigin() {
            final KidDialogV2 dialog = new KidDialogV2(mContext, themeId);
            dialog.setCancelable(cancelable);
            if (contentView != null) {
                dialog.setContentView(contentView);
                dialog.showOrigin();
                return dialog;
            }
            return null;
        }

        public KidDialogV2 showFromBottom() {
            final KidDialogV2 dialog = new KidDialogV2(mContext, themeId);
            dialog.setCancelable(cancelable);
            if (contentView != null) {
                dialog.setContentView(contentView);
                dialog.showFromBottom();
                return dialog;
            }
            return null;
        }

        public KidDialogV2 show() {
            final KidDialogV2 dialog = new KidDialogV2(mContext, themeId);
            dialog.setCancelable(cancelable);
            if (contentView != null) {
                dialog.setContentView(contentView);
                dialog.show();
                return dialog;
            }
            View view = LayoutInflater.from(mContext).inflate(R.layout.layout_material_dialog, null);
            TextView tvTitle = view.findViewById(R.id.title);
            TextView tvContent = view.findViewById(R.id.message);
            AppCompatButton btnPositive = view.findViewById(R.id.btn_p);
            AppCompatButton btnNegative = view.findViewById(R.id.btn_n);

            if (!TextUtils.isEmpty(title)) {
                tvTitle.setText(title);
                tvTitle.setVisibility(View.VISIBLE);
            } else {
                tvTitle.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(content)) {
                tvContent.setText(content);
                tvContent.setVisibility(View.VISIBLE);
            } else if (contentWeb != null) {
                tvContent.setText(contentWeb);
                tvContent.setVisibility(View.VISIBLE);
            } else {
                tvContent.setVisibility(View.GONE);
            }

            if (!TextUtils.isEmpty(positiveText)) {
                btnPositive.setText(positiveText);
                btnPositive.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog.dismiss();
                        if (positiveClickedListener != null) {
                            positiveClickedListener.onClick(view);
                        }
                    }
                });
                btnPositive.setVisibility(View.VISIBLE);
            } else {
                btnPositive.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(negativeText)) {
                btnNegative.setText(negativeText);
                btnNegative.setVisibility(View.VISIBLE);
                btnNegative.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog.dismiss();
                        if (negativeClickedListener != null) {
                            negativeClickedListener.onClick(view);
                        }
                    }
                });
            } else {
                btnNegative.setVisibility(View.GONE);
            }
            dialog.setContentView(view);
            try {
                dialog.show();
            } catch (Exception ignore) {
                ignore.printStackTrace();
            }
            return dialog;
        }

        public KidDialogV2 getDialog() {
            final KidDialogV2 dialog = new KidDialogV2(mContext, themeId);
            dialog.setCancelable(cancelable);
            if (contentView != null) {
                dialog.setContentView(contentView);
                // dialog.show();
                return dialog;
            }
            View view = LayoutInflater.from(mContext).inflate(R.layout.layout_material_dialog, null);
            TextView tvTitle = view.findViewById(R.id.title);
            TextView tvContent = view.findViewById(R.id.message);
            AppCompatButton btnPositive = view.findViewById(R.id.btn_p);
            AppCompatButton btnNegative = view.findViewById(R.id.btn_n);

            if (!TextUtils.isEmpty(title)) {
                tvTitle.setText(title);
                tvTitle.setVisibility(View.VISIBLE);
            } else {
                tvTitle.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(content)) {
                tvContent.setText(content);
                tvContent.setVisibility(View.VISIBLE);
            } else if (contentWeb != null) {
                tvContent.setText(contentWeb);
                tvContent.setVisibility(View.VISIBLE);
            } else {
                tvContent.setVisibility(View.GONE);
            }

            if (!TextUtils.isEmpty(positiveText)) {
                btnPositive.setText(positiveText);
                btnPositive.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog.dismiss();
                        if (positiveClickedListener != null) {
                            positiveClickedListener.onClick(view);
                        }
                    }
                });
                btnPositive.setVisibility(View.VISIBLE);
            } else {
                btnPositive.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(negativeText)) {
                btnNegative.setText(negativeText);
                btnNegative.setVisibility(View.VISIBLE);
                btnNegative.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog.dismiss();
                        if (negativeClickedListener != null) {
                            negativeClickedListener.onClick(view);
                        }
                    }
                });
            } else {
                btnNegative.setVisibility(View.GONE);
            }
            dialog.setContentView(view);
            return dialog;
        }

    }
}
